            import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/LoginView.vue')
  },
  {
    // 主布局
    path: '/',
    component: () => import('@/views/layout/MainLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/DashboardView.vue'),
        meta: { title: '首页', requiresAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/profile/ProfileView.vue'),
        meta: { title: '个人中心', requiresAuth: true }
      },
      {
        path: 'my-certificates',
        name: 'MyCertificates',
        component: () => import('@/views/profile/MyCertificates.vue'),
        meta: { title: '我的证书', requiresAuth: true }
      },
      // 竞赛相关路由
      {
        path: 'contest',
        children: [
          {
            path: 'list',
            name: 'ContestList',
            component: () => import('@/views/contest/ContestList.vue'),
            meta: { 
              title: '竞赛列表',
              requiresAuth: true 
            }
          },
          {
            path: 'my-contests',
            name: 'MyContests',
            component: () => import('@/views/contest/MyContests.vue'),
            meta: { 
              title: '我的竞赛',
              requiresAuth: true 
            }
          },
          {
            path: 'detail/:id',
            name: 'ContestDetail',
            component: () => import('@/views/contest/ContestDetail.vue'),
            meta: {
              title: '竞赛详情',
              requiresAuth: true
            }
          },
          {
            path: 'ranking/:id',
            name: 'ContestRanking',
            component: () => import('@/views/contest/ContestRanking.vue'),
            meta: {
              title: '竞赛排名',
              requiresAuth: true
            }
          },
          {
            path: 'create',
            name: 'ContestCreate',
            component: () => import('@/views/contest/ContestCreate.vue'),
            meta: {
              title: '创建竞赛',
              requiresAuth: true
            }
          },
          {
            path: 'questions',
            name: 'QuestionManagement',
            component: () => import('@/views/contest/QuestionManagement.vue'),
            meta: {
              title: '题目管理',
              requiresAuth: true
            }
          },
          {
            path: 'question/create/:contestId?',
            name: 'QuestionCreate',
            component: () => import('@/views/contest/QuestionCreate.vue'),
            meta: {
              title: '创建题目',
              requiresAuth: true
            }
          },
          {
            path: 'question/edit/:id',
            name: 'QuestionEdit',
            component: () => import('@/views/contest/QuestionEdit.vue'),
            meta: {
              title: '编辑题目',
              requiresAuth: true
            }
          },
          {
            path: 'realtime/:id',
            name: 'RealTimeContest',
            component: () => import('@/views/contest/RealTimeContest.vue'),
            meta: {
              title: '实时答题',
              requiresAuth: true
            }
          }
        ]
      },
      // 系统管理路由
      {
        path: 'admin/users',
        name: 'UserManagement',
        component: () => import('@/views/admin/UserManagement.vue'),
        meta: { title: '用户管理', icon: 'user', requiresAuth: true, roles: ['ADMIN'] }
      },
      // 管理员竞赛管理路由
      {
        path: 'admin/contests',
        name: 'ContestReview',
        component: () => import('@/views/admin/ContestReview.vue'),
        meta: { title: '竞赛审核', icon: 'trophy', requiresAuth: true, roles: ['ADMIN'] }
      },
      {
        path: 'admin/questions',
        name: 'QuestionReview',
        component: () => import('@/views/admin/QuestionReview.vue'),
        meta: { title: '题目审核', icon: 'document', requiresAuth: true, roles: ['ADMIN'] }
      },
      {
        path: 'admin/statistics',
        name: 'SystemStatistics',
        component: () => import('@/views/admin/SystemStatistics.vue'),
        meta: { title: '系统统计', icon: 'chart', requiresAuth: true, roles: ['ADMIN'] }
      }
    ]
  },
  // 旧的路由，可以删除或保留作为备份
  // {
  //   path: '/home',
  //   name: 'Home',
  //   component: () => import('@/views/home/HomeView.vue'),
  //   meta: { requiresAuth: true }
  // },
  // {
  //   path: '/profile',
  //   name: 'Profile',
  //   component: () => import('@/views/profile/ProfileView.vue'),
  //   meta: { requiresAuth: true }
  // },
  // {
  //   path: '/admin',
  //   name: 'Admin',
  //   component: () => import('@/views/admin/AdminLayout.vue'),
  //   meta: { requiresAuth: true, roles: ['ADMIN'] },
  //   children: [
  //     {
  //       path: 'users',
  //       name: 'UserManagement',
  //       component: () => import('@/views/admin/UserManagement.vue'),
  //       meta: { title: '用户管理', icon: 'user', requiresAuth: true, roles: ['ADMIN'] }
  //     }
  //   ]
  // },
  // // 竞赛相关路由
  // {
  //   path: '/contest',
  //   component: () => import('@/views/contest/ContestLayout.vue'),
  //   meta: { requiresAuth: true },
  //   children: [
  //     {
  //       path: 'list',
  //       name: 'ContestList',
  //       component: () => import('@/views/contest/ContestList.vue'),
  //       meta: { title: '竞赛广场', requiresAuth: true }
  //     },
  //     {
  //       path: 'detail/:id',
  //       name: 'ContestDetail',
  //       component: () => import('@/views/contest/ContestDetail.vue'),
  //       meta: { title: '竞赛详情', requiresAuth: true }
  //     },
  //     {
  //       path: 'ranking/:id',
  //       name: 'ContestRanking',
  //       component: () => import('@/views/contest/ContestRanking.vue'),
  //       meta: { title: '竞赛排名', requiresAuth: true }
  //     }
  //   ]
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((route, from, next) => {
  const token = localStorage.getItem('token')
  
  // 检查是否需要登录认证
  if (route.meta.requiresAuth && !token) {
    next('/login')
    return
  }
  
  // 检查角色权限
  if (route.meta.roles) {
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
    const userRole = userInfo.roleCode
    
    // 如果用户没有角色信息或角色不匹配
    if (!userRole || !route.meta.roles.includes(userRole)) {
      next('/dashboard')
      return
    }
  }
  
  next()
})

export default router 